package com.llj.Road_curve_measuring_system.module.input;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

import com.llj.Road_curve_measuring_system.base.BasePresenter;
import com.llj.Road_curve_measuring_system.common.GlobalConstant;
import com.llj.Road_curve_measuring_system.util.MyUtil;
import com.llj.Road_curve_measuring_system.util.SpUtil;
import com.llj.Road_curve_measuring_system.util.ToastUtil;

/**
 * @author ：Dyj
 * @date ：Created in 2022/4/17 14:11
 * @description：Input的presenter层
 * @modified By：
 * @version: 1.0
 */
public class InputPresenter extends BasePresenter<InputView> {

    public InputPresenter(InputView baseView) {
        super(baseView);
    }

    /**
     * 数值计算
     *
     * @param id 对应哪一个计算方法
     * @param min ′
     * @param r 半径
     * @param jd 交点桩号
     * @param interval 间隔距离
     * @param xxx 缓和曲线长，暂不知名，用xxx代替，无需更改
     */
    void calculation(int id , String min,  String r, String jd, String interval,String xxx,
                     String jdx,String jdy,String jdZy){
        //当来自圆曲线时，不让缓和曲线长赋值为空
        if (id == GlobalConstant.CCDM || id == GlobalConstant.CCTS ){
            xxx = interval;
            jdx = interval;
            jdy = interval;
            jdZy = interval;
        }else if (id == GlobalConstant.MCDM || id == GlobalConstant.MCTS ){
            jdx = interval;
            jdy = interval;
            jdZy = interval;
        }
        if (id == GlobalConstant.CCPV || id ==  GlobalConstant.CCCS) {
            xxx = interval;
        }
        if (isValid(min,r,jd,interval,xxx,jdx,jdy,jdZy,12)) {
            double min1 = Double.parseDouble(min);
            double r1 = Double.parseDouble(r);
            double jd1 = Double.parseDouble(jd);
            double interval1 = Double.parseDouble(interval);
            double xxx1 = Double.parseDouble(xxx);
            double jdx1 = Double.parseDouble(jdx);
            double jdy1 = Double.parseDouble(jdy);
            double jdZy1 = Double.parseDouble(jdZy);
            // TODO: 2022/5/14   在此处的switch()中添加计算方法,对应处为空填入Double.NaN
            //根据id来启用对应的计算方法
            switch (id) {
                case GlobalConstant.CCDM:
                    InputModel.ypjCompute(min1, r1, jd1, interval1);
                    MyUtil.saveDataToSp(min1,r1,jd1,interval1,Double.NaN,Double.NaN,Double.NaN,Double.NaN);
                    break;
                case GlobalConstant.MCDM:
                    InputModel.hpjCompute(min1, r1, jd1, interval1,xxx1);
                    MyUtil.saveDataToSp(min1,r1,jd1,interval1,xxx1,Double.NaN,Double.NaN,Double.NaN);
                    break;
                case GlobalConstant.CCTS:
                    InputModel.yqxCompute(min1, r1, jd1, interval1);
                    MyUtil.saveDataToSp(min1,r1,jd1,interval1,Double.NaN,Double.NaN,Double.NaN,Double.NaN);
                    break;
                case GlobalConstant.MCTS:
                    InputModel.hqxCompute(min1,r1,jd1, interval1, xxx1);
                    MyUtil.saveDataToSp(min1,r1,jd1,interval1,xxx1,Double.NaN,Double.NaN,Double.NaN);
                    break;
                case GlobalConstant.CCPV:
                    InputModel.yjiCompute(min1, r1, jd1, interval1, jdx1, jdy1, jdZy1);
                    MyUtil.saveDataToSp(min1,r1,jd1,interval1,Double.NaN,jdx1,jdy1,jdZy1);
                    break;
                case GlobalConstant.MCPV:
                    InputModel.hjiCompute(min1, r1, jd1, interval1, jdx1, jdy1, jdZy1, xxx1);
                    MyUtil.saveDataToSp(min1,r1,jd1,interval1,xxx1,jdx1,jdy1,jdZy1);
                    break;
                case GlobalConstant.CCCS:
                    InputModel.ycsCompute(min1, r1, jd1, interval1, jdx1, jdy1, jdZy1);
                    MyUtil.saveDataToSp(min1,r1,jd1,interval1,Double.NaN,jdx1,jdy1,jdZy1);
                    break;
                case GlobalConstant.MCCS:
                    InputModel.hcsCompute(min1, r1, jd1, interval1, jdx1, jdy1, jdZy1, xxx1);
                    MyUtil.saveDataToSp(min1,r1,jd1,interval1,xxx1,jdx1,jdy1,jdZy1);
                    break;
                default:
                    break;
            }
            baseView.doSuccess();
            baseView.SuccessHideLoading();
        }else {
            baseView.FailedHideLoading();
        }
    }

    /**
     * 数据是否合规格
     *
     * @param min ′
     * @param r 半径
     * @param jd 交点桩号
     * @param interval 间隔距离
     * @param xxx 缓和曲线长，暂不知名，用xxx代替，无需更改
     * @param len 输入数值允许最大的长度
     * @return true or false,满足所有数据验证就true
     */
    private boolean isValid( String min, String r, String jd,
                            String interval,String xxx,String jdx,String jdy,String jdZy, int len) {
        return  check(min,len) && check(r,len)
                && check(jd,len) && check(interval,len) && check(xxx,len) && check(jdx,len)
                && check(jdy,len) && check(jdZy,len);
    }

    /**
     * 单个数据查验
     *
     * @param sum 输入的值
     * @param len 最大长度
     * @return true or false,满足数据验证就true
     */
    private boolean check(String sum, int len) {

        return !TextUtils.isEmpty(sum) && String.valueOf(sum).length() <= len;
    }

}
